package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
@RequestMapping("/api/item")
public class ItemApiController {
    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    //获取商品详情页面所需要的数据 汇总  JUC == 多线程 + 锁  异步编排
    @GetMapping("/getItem/{skuId}")
    public Map getItem(@PathVariable Long skuId){
        Map<Object, Object> result = new HashMap<>();
        //1.skuInfo
        CompletableFuture<SkuInfo> getSkuInfo = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            result.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);


        //2.123级分类
        CompletableFuture<Void> getBaseCategoryView = getSkuInfo.thenAcceptAsync((skuInfo) -> {
            BaseCategoryView baseCategoryView = productFeignClient.getBaseCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView", baseCategoryView);
        });

        //3.实时价格
        CompletableFuture<Void> getPrice = CompletableFuture.runAsync(() -> {
            BigDecimal price = productFeignClient.getPrice(skuId);
            result.put("price", price);
        });

        //4.销售属性以及属性值选中
        CompletableFuture<Void> getSpuSaleAttrListCheckBySku = getSkuInfo.thenAcceptAsync((skuInfo) -> {
            List<SpuSaleAttr> list = productFeignClient.getSpuSaleAttrListCheckBySku(skuInfo.getSpuId(), skuInfo.getId());
            result.put("spuSaleAttrList", list);
        });

        //5.组合与对应的sku
        CompletableFuture<Void> getSkuValueIdsMap = getSkuInfo.thenAcceptAsync((skuInfo) -> {
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            result.put("valuesSkuJson", JSONObject.toJSONString(skuValueIdsMap));
        });

        //6.加分
        CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId,1);
        },threadPoolExecutor);
        CompletableFuture.allOf(getSkuInfo,getBaseCategoryView,getPrice,getSpuSaleAttrListCheckBySku
        ,getSkuValueIdsMap).join();
        return result;
    }
    @Autowired
    private ListFeignClient listFeignClient;
}
